/*
 * This file is part of jboss-admin-rest
 * Copyright 2009, Christos Vasilakis (cvasilak@gmail.com)
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.admin.rest.domain.components;

import org.jboss.admin.rest.domain.common.ManagedPropertyBean;
import org.jboss.managed.api.ManagedComponent;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author cvasilak
 */
@XmlRootElement(name="datasource")
@XmlType(propOrder = {"name", "type", "subtype", "status", "url", "properties"})
@XmlAccessorType(XmlAccessType.NONE)
public class ManagedComponentDataSourceBean extends ManagedComponentBean {

	/* DataSource Properties
	 * (disclaimer: property descriptions were retrieved from built-in descrs and embjopr web app)
	 */
	enum Property {
		DRIVER_CLASS("driver-class", "The DataSource connection driver class name"),
		USERNAME("user-name", "The DataSource username"),
		PASSWORD("password", "The DataSource password"),
		JNDI_NAME("jndi-name", "The global JNDI name to bind the factory under"),
		CONN_URL("connection-url", "The DataSource connection URL"),
		CON_INUSE_COUNT("inUseConnectionCount", "the number of connections that are currently in use"),
		MAX_CON_INUSE_COUNT("maxConnectionsInUseCount", "the most connections that have been simultaneously in use since this datasource was started"),
		CON_CREATED_COUNT("connectionCreatedCount", "the number of connections that have been created since the datasource was last started"),
		CON_COUNT("connectionCount", "the number of connections that are currently in the pool"),
		CON_DESTROYED_COUNT("connectionDestroyedCount", "the number of connections that have been destroyed since the datasource was last started"),
		CON_AVAIL_COUNT("availableConnectionCount", "the maximum number of connections that are available"),
		/* TODO:
		 * deterimine the difference between [min,max]-size with [min,max]-pool-size
		 */
		MIN_SIZE("minSize", "Min size"),
		MAX_SIZE("maxSize", "Max size");

		private final String name;
		private final String descr;

		private Property(String name, String descr) {
			this.name = name;
			this.descr = descr;
		}

		public String getName() {
			return name;
		}

		public String getDescription() {
			return descr;
		}
	}

	public ManagedComponentDataSourceBean() {
	}
	
	public ManagedComponentDataSourceBean(ManagedComponent component) {
		super(component);
	}

	@XmlElementWrapper(name = "properties")
	@XmlElement(name = "property")
	public List<ManagedPropertyBean> getProperties() {
		List<ManagedPropertyBean> properties = new ArrayList<ManagedPropertyBean>();

		for (Property property : Property.values()) {
			properties.add(new ManagedPropertyBean(component.getProperty(property.getName()), property.getDescription()));
		}
		
		return properties;
	}
}
